import { AbsFormUtil } from '@abstract/form';
import { Component, Input, OnInit } from '@angular/core';
import { FormBuilder, Validators } from '@angular/forms';
import { BaseService } from '@service/base.service';
import { NzModalRef } from 'ng-zorro-antd/modal';

@Component({
	selector: 'app-base-schedule-edit',
	templateUrl: './base-schedule-edit.component.html',
	styleUrls: ['./base-schedule-edit.component.less']
})
export class BaseScheduleEditComponent extends AbsFormUtil implements OnInit {
	@Input() id: number;
	constructor(
		private fb: FormBuilder,
		private nzModalRef: NzModalRef,
		private baseService: BaseService
	) {
		super();
	}


	ngOnInit() {
		this.buildForm();
		if (this.id){
			this.getScheduleInfo();
		}
	}
	buildForm() {
		this.validateForm = this.fb.group({
			beanName: ['', Validators.required],
			cronExpression: ['', [Validators.required]],
			params: [''],
			remark: [''],
		});
	}

	onSubmit() {
		this.markAsDirty();
		if (this.validateForm.invalid) {
			return;
		}
		const formValue = this.validateForm.value;
		return {...formValue};
	}
	destroyModal() {
		this.nzModalRef.destroy();
	}







	private async getScheduleInfo() {
		const result = await this.baseService.getScheduleInfo(this.id);
		this.validateForm.patchValue(result.data);
	}
}
